import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description: leetcode单词规律，HashMap练习题
 * 两个字符串中的字符和字符串一一映射，且规律一致
 */
class Solution {
    public static boolean wordPattern(String pattern, String s) {
        if(pattern == null || s == null) {
            return false;
        }
        HashMap<Character, String> map1 = new HashMap<>();
        HashMap<String, Character> map2 = new HashMap<>();
        String[] string = s.split(" ");
        int pLength = pattern.length();
        int i = 0;
        for(; i < pLength; i++) {
            if(i < string.length) {
                if(map1.containsKey(pattern.charAt(i))) {
                    if(!(map1.get(pattern.charAt(i)).equals(string[i]))) {
                        return false;
                    }
                }else {
                    map1.put(pattern.charAt(i), string[i]);
                }
                if(map2.containsKey(string[i])) {
                    if(!(map2.get(string[i]).equals(pattern.charAt(i)))) {
                        return false;
                    }
                }else {
                    map2.put(string[i], pattern.charAt(i));
                }
            }else {
                break;
            }
        }
        if(i < pLength || i < string.length) {
            return false;
        }
        return true;
    }
}

public class Test3 {
    public static void main(String[] args) {
        boolean flg = Solution.wordPattern("jquery", "jquery");
        System.out.println(flg);
    }
}
